package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.OrderInfo;
import cn.wolfcode.shop.api.domain.OrderProduct;
import cn.wolfcode.shop.api.service.IOrderActionService;
import cn.wolfcode.shop.api.service.IOrderProductService;
import cn.wolfcode.shop.api.service.IOrderinfoService;
import cn.wolfcode.shop.api.vo.DeliverAddressVo;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import cn.wolfcode.shop.api.vo.OrderInfoVo;
import cn.wolfcode.shop.api.vo.ProductValueVo;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value = "订单接口", description = "提供查看订单相关的信息")
@RestController
@RequestMapping("api/orderInfo")
public class OrderInterfaceController {
    @Reference
    private IOrderinfoService orderinfoService;
    @Reference
    private IOrderProductService orderProductService;
    @Reference
    private IOrderActionService orderActionService;

    @ApiOperation(value = "订单详细", notes = "根据传进来的订单id获取到订单的详细信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path", dataType = "Long", required = true)
    )
    @RequestMapping(value = "orderDetail/{orderId}", method = RequestMethod.POST)
    public JsonResultVo orderList(@PathVariable(value = "orderId") Long orderId){
        JsonResultVo resultVo = new JsonResultVo();
        try {
            OrderInfo orderInfo = orderinfoService.get(orderId);
            List<OrderProduct> products = orderProductService.selectProductByOrderId(orderId);
            OrderInfoVo orderInfoVo = new OrderInfoVo();
            orderInfoVo.setOrderId(orderInfo.getId());
            orderInfoVo.setOrderSn(orderInfo.getOrderSn());
            orderInfoVo.setOrderState(orderInfo.getOrderStatusDisplay());
            orderInfoVo.setBuyTime(orderInfo.getOrderTime());
            orderInfoVo.setTotalPrice(orderInfo.getOrderAmount());
            List<ProductValueVo> productValueVos = new ArrayList<>();
            DeliverAddressVo addressVo = new DeliverAddressVo();
            addressVo.setDeliverUser(orderInfo.getConsignee());
            addressVo.setDeliverPhone(orderInfo.getMobile());
            addressVo.setZipcode(orderInfo.getZipcode());
            addressVo.setDeliverAddressFullString(orderInfo.getProvince()+orderInfo.getCity()+orderInfo.getDistrict()+orderInfo.getAddress());
            orderInfoVo.setAddressVo(addressVo);
            for (OrderProduct product : products) {
                ProductValueVo valueVo = new ProductValueVo();
                valueVo.setProductName(product.getProductName());
                valueVo.setPrice(product.getProductPrice());
                valueVo.setNumber(product.getProductNumber());
                valueVo.setSkuProperty(product.getSkuAttr());
                productValueVos.add(valueVo);
            }
            orderInfoVo.setItems(productValueVos);
            resultVo.setResult(true);
            resultVo.setResult(orderInfoVo);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }
    @ApiOperation(value = "取消订单", notes = "根据传进来的订单id取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", paramType = "form", dataType = "Long", required = true),
            @ApiImplicitParam(name = "state", value = "取消状态", paramType = "form", dataType = "Byte", required = true),
            @ApiImplicitParam(name = "remark", value = "取消原因", paramType = "form", dataType = "String", required = true)
    })
    @RequestMapping(value = "order_cancel", method = RequestMethod.POST)
    public JsonResultVo order_cancel(Long id, Byte state, String remark){
        JsonResultVo resultVo = new JsonResultVo();
        try {
            orderinfoService.changeOrderStatus(id, state, remark, "");
            resultVo.setResult(true);
            resultVo.setErrorMsg("取消订单成功");
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setSuccess(false);
            resultVo.setErrorMsg(e.getMessage());
        }
        return resultVo;
    }
}

